﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin.Controls
{
    public partial class EditTienIch : System.Web.UI.UserControl
    {
        DataTable dt;
        public string name;
        public string image;
        public int slideid;
        public string filePathNewsIamge;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                slideid = Convert.ToInt32(Request.QueryString["newsid"]);
                dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_DanhMucTienIch", "DanhMucTienIch_ID", slideid.ToString());
                image = dt.Rows[0]["DanhMucTienIch_Image"].ToString();
                name = dt.Rows[0]["DanhMucTienIch_Name"].ToString(); ;
                txtName.Text = name;
            }
            catch (Exception)
            {
                Response.Redirect("/Admin/error.htm");
            }
        }

        protected void btnUpdateSlide_Click(object sender, EventArgs e)
        {
            if (txtName.Text != "")
            {
                if (fulImageNews.HasFile)
                {
                    if (LIB.Validate.CheckFileType(fulImageNews.FileName))
                    {
                        LIB.Validate saveimage = new LIB.Validate();
                        filePathNewsIamge = "/Resources/Images/Images-TienIch/" + fulImageNews.FileName + "";
                        saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Images-TienIch/"), fulImageNews.FileName, 200, fulImageNews.PostedFile.InputStream);
                        //File.Delete(MapPath(image));
                        DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[2];
                        cl[0] = new DAL.DHelpers.Column("DanhMucTienIch_Image", filePathNewsIamge, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                        cl[1] = new DAL.DHelpers.Column("DanhMucTienIch_Name", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                        string[] colWhere = new string[1];
                        colWhere[0] = "DanhMucTienIch_ID";

                        object[] colValue = new object[1];
                        colValue[0] = slideid;

                        DAL.DCollector.DBCollector.Instance.Update("tbl_DanhMucTienIch", cl, colWhere, colValue);
                        Response.Redirect("/Admin/Index.aspx?panel=1&sub=4");
                    }
                    else
                    {
                        Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                    }
                }
                else
                {
                    DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[1];
                    cl[0] = new DAL.DHelpers.Column("DanhMucTienIch_Name", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                    string[] colWhere = new string[1];
                    colWhere[0] = "DanhMucTienIch_ID";

                    object[] colValue = new object[1];
                    colValue[0] = slideid;

                    DAL.DCollector.DBCollector.Instance.Update("tbl_DanhMucTienIch", cl, colWhere, colValue);
                    Response.Redirect("/Admin/Index.aspx?panel=1&sub=4");
                }
            }
            else
            {
                Response.Write("<script>alert('Bạn vui lòng nhập tên tiện ích')</script>");
            }
        }
    }
}